package com.shaozi.nc.server.rpc;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.shaozi.data.mongo.model.ModelBase;
import com.shaozi.data.mq.business.bean.MqTargetUserBean;
import com.shaozi.data.rpc.base.RpcResultBean;
import com.shaozi.data.rpc.server.RpcServerBase;
import com.shaozi.lib.core.StatusCode;
import com.shaozi.lib.throwable.SzException;
import com.shaozi.nc.bean.resp.ReqListSecretaryBean;
import com.shaozi.nc.bean.resp.ReqMakeSecretaryReadByIdsBean;
import com.shaozi.nc.bean.resp.ReqMessageSetReadForTypeBean;
import com.shaozi.nc.bean.resp.ReqSecretaryTypeBadgeBean;
import com.shaozi.nc.bean.resp.ReqSecretaryUnreadMessageTotalBean;
import com.shaozi.nc.bean.resp.RespMessageSetReadForTypeBean;
import com.shaozi.nc.bean.resp.RespSecretaryTypeBadgeBean;
import com.shaozi.nc.bean.resp.RespSecretaryUnreadMessageTotalBean;
import com.shaozi.nc.bean.resp.SecretaryMsgBean;
import com.shaozi.nc.data.model.BaseSecretaryResponseModel;
import com.shaozi.nc.data.model.SecretaryModel;
import com.shaozi.nc.manage.secretary.BaseSecretaryManage;
import com.shaozi.nc.utils.SecretaryHelper;

public abstract class BaseSecretaryRpcServer<T extends ModelBase> extends RpcServerBase {

	private Logger logger = Logger.getLogger(BaseSecretaryRpcServer.class);

	public BaseSecretaryRpcServer() {

	}

	public RpcResultBean makeSecretaryRead(ReqMakeSecretaryReadByIdsBean reqBean) throws SzException {
		RpcResultBean ret = new RpcResultBean();
		RespMessageSetReadForTypeBean respBean = new RespMessageSetReadForTypeBean();
		boolean ok = onManange().makeSecretaryRead(reqBean, respBean);
		if (!ok) {
			ret.setCode(-1);
		}
		return ret;
	}

	@Deprecated
	public RpcResultBean getTypeBadge(ReqSecretaryTypeBadgeBean reqBean) throws SzException {
		RpcResultBean ret = new RpcResultBean();
		Map<String, RespSecretaryTypeBadgeBean> resps = onManange().getTypeBadge(reqBean);
		if ((resps == null) || (resps.isEmpty()))
			return ret;
		for (RespSecretaryTypeBadgeBean resp : resps.values()) {
			if (resp.getSecretary() != null) {
				SecretaryMsgBean msgBean = SecretaryHelper.secretaryModelToMsgBean(resp.getSecretary(),
						reqBean.getUserId());
				if (msgBean != null) {
					resp.setMsgBean(msgBean);
				}
				resp.setSecretary(null);
			}
		}
		ret.setData(resps);
		return ret;
	}

	public RpcResultBean unreadMessageTotal(ReqSecretaryUnreadMessageTotalBean reqBean) throws SzException {
		RpcResultBean ret = new RpcResultBean();
		RespSecretaryUnreadMessageTotalBean resp = (RespSecretaryUnreadMessageTotalBean) onManange()
				.unreadMessageTotal(reqBean);
		if (resp == null)
			return ret;

		SecretaryMsgBean msgBean = SecretaryHelper.secretaryModelToMsgBean(resp.getSecretary(), reqBean.getUserId());
		if (msgBean != null) {
			resp.setMsgBean(msgBean);
		}
		resp.setSecretary(null);
		ret.setData(resp);
		return ret;

	}

	public RpcResultBean messageSetReadForType(ReqMessageSetReadForTypeBean reqBean) {
		RpcResultBean ret = new RpcResultBean();
		if (!validMessageSetReadForType(ret, reqBean))
			return ret;
		RespMessageSetReadForTypeBean respBean = new RespMessageSetReadForTypeBean();
		try {
			boolean ok = onManange().makeSecretaryRead(reqBean, respBean);
			ret.setData(respBean);
		} catch (SzException e) {
			ret.setCode(StatusCode.Code_SQL_EXEC);
		}
		return ret;
	}

	private boolean validMessageSetReadForType(RpcResultBean ret, ReqMessageSetReadForTypeBean reqBean) {
		if (reqBean == null) {
			ret.setCode(StatusCode.Code_RPC_ReqParam_Error);
			return false;
		}
		if (StringUtils.isEmpty(reqBean.getCompanyId())) {
			ret.setCode(StatusCode.Code_RPC_ReqParam_Error);
			return false;
		}
		if (StringUtils.isEmpty(reqBean.getUserId())) {
			ret.setCode(StatusCode.Code_RPC_ReqParam_Error);
			return false;
		}
		return true;
	}

	public abstract RpcResultBean listUpdate(ReqListSecretaryBean reqBean);

	protected void setBaseSecretaryResponseModel(BaseSecretaryResponseModel model, boolean isInit,
			List<SecretaryModel> secretarys, ReqListSecretaryBean reqBean) throws SzException {
		String userId = reqBean.getUserId();
		if (isInit) {
			model.setIncreaseId(onManange().getMaxUpdateTime(reqBean.getCompanyId(), userId,reqBean.getIsRead()));
			if (secretarys.size() > 0) {
				model.setHistoryId(getUserTime(secretarys.get(secretarys.size() - 1), "createTime", userId));
			}
		} else if (reqBean.getDirect() == ReqListSecretaryBean.Direct_New) {
			if (secretarys.size() > 0) {
				model.setIncreaseId(getUserTime(secretarys.get(secretarys.size() - 1), "updateTime", userId));
			}
		} else {
			if (secretarys.size() > 0) {
				model.setHistoryId(getUserTime(secretarys.get(secretarys.size() - 1), "createTime", userId));
			}
		}
	}

	private long getUserTime(SecretaryModel secretary, String fieldName, String userId) {
		if (null == secretary.getUsers() || secretary.getUsers().isEmpty())
			return 0l;
		for (MqTargetUserBean user : secretary.getUsers()) {
			if (user.getUserId().equals(userId)) {
				try {
					return Long.parseLong(BeanUtils.getProperty(user, fieldName));
				} catch (NumberFormatException | IllegalAccessException | InvocationTargetException
						| NoSuchMethodException e) {
					logger.error(SzException.getStackMessage(e));
					return 0L;
				}
			}
		}
		return 0L;
	}

	public abstract <T extends ModelBase> BaseSecretaryManage onManange();
}
